import router from '@/router'
import store from '@/store'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']

// 全局前置守卫
router.beforeEach(async(to, from, next) => {
  nprogress.start() // 开启进度条
  const { token } = store.getters
  // 如果有token 去的是登录页，就跳到主页
  if (token) {
    if (to.path === '/login') {
      next('/')
      nprogress.done() // 不会走路由后置守卫
    } else {
      // if (!store.getters.userId) {
      //   await store.dispatch('user/getUserinfo')
      // }
      next()
    }
  } else {
    // 如果没有 token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nprogress.done()
    }
  }
})

router.afterEach(() => {
// 关闭进度条
  nprogress.done()
})
